<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="addProjectBalance" type="primary" icon="plus" style="width: 100%">新增</a-button>
      <!--<a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>-->
    </div>

    <!-- table区域-begin -->
    <div>
      <!--<div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>-->

      <div class="list">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoadList"
        >
          <div
            class="customer-list"
            v-for="(item,i) in dataSource"
            @click="contractDetail(item.customerId)"
            :key="i">
            <div>
              <div class="name">客户姓名：{{item.customerName || ''}}</div>
              <div class="contact">结算单编号：{{item.balanceAccountsNo || ''}}</div>
              <div class="contact">
                <span>结算时间：{{ item.balanceAccountsTime || '无' }}</span>
              </div>
              <div class="contact">
                <span>项目名称：{{ item.projectName || '无'}}</span>
                <span style="float: right">
                  <a @click.stop="editProjectBalance(item)" v-if="item.status == 1">编辑</a>
                  <a-divider type="vertical" v-if="item.status == 1"/>
                  <a @click.stop="makeCollections(item)" v-if="item.status == 3 && item.toBeCollectedMoney != 0">收款</a>
                </span>
              </div>
              <div class="contact">
                <span>结算名称：{{ item.balanceAccountsName || '无'}}</span>
              </div>
              <div class="contact">
                <span>金额：{{ item.money || '无'}}</span>
                <span style="margin-left: 20px">状态：
                  <a-tag v-if="item.status === 1">草稿</a-tag>
                  <a-tag color="orange" v-if="item.status === 2">待审批</a-tag>
                  <a-tag color="green" v-if="item.status === 3">审批通过</a-tag>
                  <a-tag color="red" v-if="item.status === 4">审批未通过</a-tag>
                </span>
              </div>
              <div class="contact">
                <span>已核销金额：{{ item.writtenOffMoney || 0}}</span>
                <span style="margin-left: 20px">待核销金额：{{ item.toBeCollectedMoney || '无'}}</span>
              </div>
            </div>
          </div>
        </van-list>
      </div>

      <!--<a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <a slot="balanceAccountsNo" @click="contractDetail(record.customerId)" slot-scope="text,record">{{ text }}</a>

        <span slot="action" slot-scope="text, record">
          <a @click="editProjectBalance(record)" v-if="record.status == 1">编辑</a>
          <a-divider type="vertical" v-if="record.status == 1"/>
          <a @click="makeCollections(record)" v-if="record.status == 3 && record.toBeCollectedMoney != 0">收款</a>
        </span>

      </a-table>-->
    </div>

    <add-project-balance-modal ref="addProjectBalanceModal" @ok="modalOk"></add-project-balance-modal>
    <add-make-collections-modal ref="addMakeCollectionsModal" @ok="modalOk"></add-make-collections-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import addProjectBalanceModal from "../../customer/info/tab/modules/addProjectBalanceModal";
  import addMakeCollectionsModal from "../../customer/info/tab/modules/addMakeCollectionsModal";
  import { getAction } from "@api/manage";

  export default {
    name: 'balanceAccountsList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      addMakeCollectionsModal,
      addProjectBalanceModal,
    },
    data () {
      return {
        description: '项目结算表管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'结算单编号',
            align:"center",
            dataIndex: 'balanceAccountsNo',
            slots: { title: 'customTitle' },
            scopedSlots: { customRender: 'balanceAccountsNo' },
          },
          {
            title:'客户姓名',
            align:"center",
            dataIndex: 'customerName'
          },
          {
            title:'结算时间',
            align:"center",
            dataIndex: 'balanceAccountsTime'
          },
          {
            title:'项目名称',
            align:"center",
            dataIndex: 'projectName'
          },
          {
            title:'结算名称',
            align:"center",
            dataIndex: 'balanceAccountsName'
          },
          {
            title:'金额',
            align:"center",
            dataIndex: 'money'
          },
          {
            title:'已核销金额',
            align:"center",
            dataIndex: 'writtenOffMoney',
            customRender:function (text) {
              if (text == null){
                return 0;
              }else {
                return text;
              }
            }
          },
          {
            title:'待收款金额',
            align:"center",
            dataIndex: 'toBeCollectedMoney',
          },
          {
            title:'状态',
            align:"center",
            dataIndex: 'status',
            customRender:function (text) {
              switch (text) {
                case 1:
                  return <a-tag>草稿</a-tag>
                  break;
                case 2:
                  return <a-tag color="orange">待审批</a-tag>
                  break;
                case 3:
                  return <a-tag color="green">审批通过</a-tag>
                  break;
                case 4:
                  return <a-tag color="red">审批未通过</a-tag>
                  break;
              }
            }
          },
          {
            title:'备注',
            align:"center",
            dataIndex: 'remark'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/ghCustomerProjectBalanceAccounts/list",
          delete: "/ghCustomerProjectBalanceAccounts/delete",
          deleteBatch: "/ghCustomerProjectBalanceAccounts/deleteBatch",
          exportXlsUrl: "/ghCustomerProjectBalanceAccounts/exportXls",
          importExcelUrl: "/ghCustomerProjectBalanceAccounts/importExcel",
          queryCustomerContractFile: "/ghCustomerFileInfo/queryByCustomerId",
        },
        dictOptions:{},
        superFieldList:[],
        contractFile:{},
        finished: false,
      }
    },
    created() {
      this.loadData();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      addProjectBalance(){
        this.$refs.addProjectBalanceModal.title = "新增项目结算";
        this.$refs.addProjectBalanceModal.add();
        this.$refs.addProjectBalanceModal.customerSelectFlag = true;
        //this.$refs.addProjectBalanceModal.model.customerId = this.record.id
      },
      editProjectBalance(record){
        this.$refs.addProjectBalanceModal.title = "编辑项目结算";
        this.$refs.addProjectBalanceModal.edit(record);
        this.$refs.addProjectBalanceModal.customerSelectFlag = true;
      },
      makeCollections(record){
        this.$refs.addMakeCollectionsModal.title = "新增收款";
        this.$refs.addMakeCollectionsModal.add(record);
      },
      modalOk(){
        this.loadData();
      },
      //查看正式合同
      contractDetail(customerId){
        getAction(this.url.queryCustomerContractFile,{customerId:customerId,contractType:2}).then((res) => {
          if (res.success) {
            this.contractFile = res.result;
            window.open(this.contractFile.fileUrl);
          }else {
            this.$message.error("该合同不存在!")
          }
        })
      },
      //下一页
      onLoadList(){
        this.ipagination.current++
        this.loadData(this.ipagination.current)
      },
      loadData(arg){
        //加载数据 若传入参数1则加载第一页的内容
        if (arg === 1) {
          this.ipagination.current = 1;
          this.dataSource = [];
          this.finished = false;
        }
        let params = this.getQueryParams();//查询条件
        this.loading = true
        getAction(this.url.list, params).then((res) => {
          if (res.success) {
            this.dataSource = [...this.dataSource, ...res.result.records]
            if (res.result.total) {
              this.ipagination.total = res.result.total
            } else {
              this.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
          this.loading = false
          if(res.result.records.length<10){
            this.finished = true;
          }
        }).finally(() => {
          this.loading = false
        })
      },
    }
  }
</script>
<style scoped lang="less">
  @import '~@assets/less/common.less';
  .select-pk{
    display:flex;
    align-items:center;
    margin:10px 0;
    &>span{
      font-weight: bold;
    }
    &-text{
      width: calc(100% - 6em);
      height: 30px;
      text-indent: 1em;
      line-height: 28px;
      border: 1px solid #d9d9d9;
      border-radius: 4px;
    }
  }
  .list{
    overflow-y: scroll;
    height:100vh;
    margin-top: 10px;
  }
  ::-webkit-scrollbar {
    display: none;
  }
  .customer-list{
    /*display: flex;*/
    align-items: center;
    border-top: 1px solid #d9d9d9;
    .name{
      font-size: 13px;
      color: #161616;
      line-height: normal;
      margin-top: 5px;
    }
    .contact{
      margin-top: 5px;
      font-size: 12px;
      color: #a39f9f;
      margin-bottom: 5px;
    }
  }
</style>

